package com.atos.smp.actions;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

import org.primefaces.model.DualListModel;

import net.codeissue.web.common.ServiceFinder;

import com.atos.smp.beans.Collaborator;
import com.atos.smp.beans.Project;
import com.atos.smp.business.ProjectManager;
import com.atos.smp.business.impl.ProjectManagerImpl;
import com.atos.smp.dao.ProjectDao;

@ManagedBean
@RequestScoped

public class ProjectBean implements Serializable {
	
	private ProjectManager projectManager;
	private Project newProject;
	private DualListModel<Collaborator> collaborators;

	public ProjectBean() {
		projectManager = Action.getProjectManager();
		newProject = new Project();
		List<Collaborator> source = projectManager.findAllCollaborators();
		List<Collaborator> target = new ArrayList<Collaborator>();
		setCollaborators(new DualListModel<Collaborator>(source, target));
	}

	public Project getNewProject() {
		return newProject;
	}

	public void setNewProject(Project newProject) {
		this.newProject = newProject;
	}

	public String addProject() {
		for (Collaborator c : collaborators.getTarget()) {
			newProject.addCollaborator(c);
		}
		projectManager.save(newProject);
		return "addSuccess";
	}
	public List<Collaborator> completeManager(String query){
		List<Collaborator> managers = new ArrayList<Collaborator>();
		List<Collaborator> list = projectManager.findAllManagers();
		for (Collaborator col : list) {			
			if(col.getFirstName().toLowerCase().startsWith(query.toLowerCase()) || col.getLastName().toLowerCase().startsWith(query.toLowerCase()))
				managers.add(col);
			
		}
		return managers;
	}
	
	public List<Collaborator> completeProjectManager(String query){
		List<Collaborator> managers = new ArrayList<Collaborator>();
		List<Collaborator> list = projectManager.findAllProjectManagers();
		for (Collaborator col : list) {			
			if(col.getFirstName().toLowerCase().startsWith(query.toLowerCase()) || col.getLastName().toLowerCase().startsWith(query.toLowerCase()))
				managers.add(col);
			
		}
		return managers;
	}

	public DualListModel<Collaborator> getCollaborators() {
		return collaborators;
	}

	public void setCollaborators(DualListModel<Collaborator> collaborators) {
		this.collaborators = collaborators;
	}


	

}
